bangkeapp.directive('timerbutton', function($timeout, $interval) {
	return {
		restrict: 'AE',
		transclude: true,
		scope: {
			showTimer: '=',
			onClick: '&',
			timeout: '='
		},
		link: function(scope, element, attrs) {
			scope.timer = true;
			scope.timerCount = scope.timeout;
			var counter = $interval(function() {
				scope.timerCount = scope.timerCount - 1000;
			}, 1000);

			$timeout(function() {
				scope.timer = false;
				$interval.cancel(counter);
				scope.showTimer = false;
			}, scope.timeout);
		},
		template: '<button ng-click="onClick()" ng-disabled="timer">' +
			'<span ng-transclude></span>&nbsp<span ng-if="showTimer">' +
			'({{ timerCount / 1000 }}s)</span></button>'
	};
});
bangkeapp.directive('inputOnChange', function() {
	return {
		restrict: 'A',
		link: function(scope, element, attrs) {
			var onChangeFunc = scope.$eval(attrs.inputOnChange);
			element.bind('change', onChangeFunc);
		}
	}
});
bangkeapp.directive('passForm', function($http) {
	return {
		restrict: 'EA',
		link: function(scope, ele, attr) {
			var inputDom = angular.element(ele[0].querySelector('.Jpass')); //密码框 
			var spanDoms = ele.find('span'); //光标span 
			var faguang = angular.element(ele[0].querySelector('.Jfaguang')); //发光外框 
			var that = this;
			inputDom.on('focus blur keyup', function(e) {
				e = e ? e : window.event;
				e.stopPropagation();
				
				if(e.type === 'focus') {
					var _currFocusInputLen = this.value.length === 6 ? 5 : this.value.length;
					spanDoms.eq(_currFocusInputLen).addClass('active');
					faguang.css({
						left: _currFocusInputLen * 60 + ((window.screen.width-360)/2) + 'px',
						opacity: 1
					});
				} else if (e.type === 'blur') {
					var _currBlurInputLen = this.value.length;
					spanDoms.eq(_currBlurInputLen).removeClass('active');
					faguang.css({
						opacity: 0
					});
				} else if (e.type === 'keyup') {
					//console.log(this.value); 
					//键盘上的数字键按下才可以输入 
					if(e.keyCode == 8 || (e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105)) {
						var curInputLen = this.value.length; //输入的文本内容长度 
						for(var j = 0; j < 6; j++) {
							spanDoms.eq(j).removeClass('active');
							spanDoms.eq(curInputLen).addClass('active');
							spanDoms.eq(curInputLen - 1).next().find('i').css({
								backgroundColor: 'transparent'
							});
							spanDoms.eq(curInputLen - 1).find('i').css({
								backgroundColor: '#666'
							});
							faguang.css({
								left: curInputLen * 60 + ((window.screen.width-360)/2) + 'px'
							});
						}
						if(curInputLen === 0) {
							spanDoms.find('i').css({
								backgroundColor: 'transparent'
							});
						}
						if(curInputLen === 6) {
							spanDoms.eq(5).addClass('active');
							faguang.css({
								left: ((window.screen.width-360)/2) + 300 + 'px'
							});
							//直接发起密码验证 
//							var doSubmitCallback = function() {
//								
//								spanDoms.find('i').css({
//									backgroundColor: 'transparent'
//								});
//								spanDoms.removeClass('active').eq(0).addClass('active');
//								faguang.css({
//									left: '0'
//								});
//							};
						}
					} else {
						this.value = this.value.replace(/\D/g, '');
					}
				}
			});
		}
	}
});
